﻿#region Creditos

/* Autor: Edilberto Sánchez Forero	*/
/* Email: esanchezfo@gmail.com		*/
/* Licencia: Propietario			*/
/* País: Colombia, Bogotá			*/

#endregion

#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESF.General.Entity;
using System.Data;
using ESF.General;
using ESF.SSO.Core.Entities;
using ESF.SSO.Core.Entities.Log;

#endregion


namespace ESF.SSO.Core.DataAccess
{
	public static class AdoUserSessionLog
	{
		#region Write
	
		public static void Insert (UserSessionLog pUserSessionLog)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_InsertUserSessionLog");
			
			AdoESFSSO.W.AddParameter (cmd, "pID", pUserSessionLog.ID);
			AdoESFSSO.W.AddParameter (cmd, "pUserPersonID", pUserSessionLog.User.ID);
			AdoESFSSO.W.AddParameter (cmd, "pStartDateTime", pUserSessionLog.StartDateTime);
			
			if (pUserSessionLog.User.UserTerminal != null)
			{
				AdoESFSSO.W.AddParameter (cmd, "pUserHostAddress", pUserSessionLog.User.UserTerminal.UserHostAddress);
				AdoESFSSO.W.AddCodeParameter (cmd, "pUserMachineIdentificator", pUserSessionLog.User.UserTerminal);
			}
			
			AdoESFSSO.W.Execute (cmd);
		}
		
		
		public static void UpdateEnd (UserSessionLog pUserSessionLog)
		{
			IDbCommand cmd;
			
			
			cmd = AdoESFSSO.W.CreateSPCommand ("esf_sso.P_UpdateEndUserSessionLog");
			
			AdoESFSSO.W.AddParameter (cmd, "pID", pUserSessionLog.ID);
			AdoESFSSO.W.AddParameter (cmd, "pEndDateTime", pUserSessionLog.EndDateTime);
			
			AdoESFSSO.W.Execute (cmd);
		}
		
		#endregion  


		#region Read

		public static DataTable GetUserSessionHistory (User pUser)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand ("[esf_sso].P_Rpt_UserSession_History");

			AdoESFSSO.R.AddParameter(cmd, "pUserAlias", pUser.Alias);

			return AdoESFSSO.R.GetFirstDataTable(cmd);
		}


		public static DataTable GetUsersOnline ()
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand ("[esf_sso].P_Rpt_UsersOnline");

			return AdoESFSSO.R.GetFirstDataTable (cmd);
		}


		public static DataTable GetUserSessions (UserSessionLog pSample)
		{
			IDbCommand cmd;


			cmd = AdoESFSSO.R.CreateSPCommand ("[esf_sso].P_Rpt_UserSessions");

			AdoESFSSO.R.AddParameter (cmd, "pUserAlias", pSample.User.Alias, General.Text.ComparisonTypes.LikeFull);
			AdoESFSSO.R.AddInitialDateParameter (cmd, "pStartDate", pSample.StartDateTime);
			AdoESFSSO.R.AddFinalDateParameter (cmd, "pEndDate", pSample.EndDateTime);

			return AdoESFSSO.R.GetFirstDataTable (cmd);
		}

		#endregion
	}
}
